package com.hmdp.voucher.task;

import com.hmdp.common.utils.TaskConstants;
import com.hmdp.model.utils.Mail;
import com.hmdp.msmfeign.MsmFeignClient;
import com.hmdp.voucher.service.IVoucherOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 订单的定时任务
 */
@Component
@EnableScheduling
public class OrderTask {

    @Autowired
    private MsmFeignClient msmFeignClient;

    @Autowired
    private IVoucherOrderService voucherOrderService;

    // 这个需要把用户表改进一下，需要有用户邮箱这个属性，这里我直接写死了
    private static final String TO_ONE = "2569362172@qq.com";

    // 每天0点执行方法，邮件提醒用户订单未支付，将自动删除
    @Scheduled(cron = "0 0 0 * * ?")
    public void noticeNotPaidOrder() {
        // 提醒用户订单为支付
        msmFeignClient.sendMail(this.createMail());
    }

    @Scheduled(cron = "0 0 8 * * ?")
    public void deleteNotPaidOrder() {
        // 删除未支付订单
        voucherOrderService.removeByStatus();
    }

    // 获取短信信息
    private Mail createMail() {
        Mail mail = new Mail();
        mail.setToOne(TO_ONE);
        mail.setSubject(TaskConstants.NotPAID_ORDER_SUBJECT);
        mail.setContext(TaskConstants.NotPAID_ORDER_CONTEXT);
        return mail;
    }
}
